package auctions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;


public class SimultaneousAuctionHouse extends AuctionHouse {

	
	public SimultaneousAuctionHouse(String outputFile, Vector<Agent> agents, Vector<Integer> paymentRule, Vector<Double> reserves, Vector<AuctionType> at) {
		super(outputFile, agents, paymentRule, reserves, at);
	}


	@Override
	public Results getWinners() {
		/**
		 * Asks each agent for its bid in each auction and determines the
		 * winner by keeping track of the highest bid.
		 * 
		 * NOTE: A bid class could be added which would include bid and time
		 * so that we could break tied bids by who submitted the bid first
		 */
		Vector<AuctionResults> results = new Vector<AuctionResults>();
		Vector<BidderResults> bresults = new Vector<BidderResults>();
		for (int i=0; i<_agents.size(); i++) {
			bresults.add(new BidderResults(_agents.get(i)));
		}
		
		for (int i = 0; i< _numAuctions; i++) {	
                        _auctions.get(i).getWinner();
                        AuctionResults r;
                        if(!_auctions.get(i)._ars.isEmpty()){
                            r = _auctions.get(i).getWinner().lastElement();
                            int maxBidder= r.getWinningBidder();
                            double price = r.getWinningPrice();
                            bresults.get(maxBidder).addWinning(i, price);
                        } else {
                            r= new AuctionResults(0,null,-1,i);
                        }
                        results.add(r);
		}
		
		_bresults = bresults;
		AuctionsToXML writer = new AuctionsToXML(results, bresults);
		writer.generateXML(_outputFile);
            return new Results(results, bresults);
	}


	@Override
	public AuctionHouseType getType() {
		return AuctionHouseType.SIMULTANEOUS;
	}

}
